#include <iostream>
#include <functional>
#include <string>
#include <memory>
#include "request.pb.h"
#include "include/muduo/net/TcpClient.h"
#include "include/muduo/net/EventLoopThread.h"
#include "protobuf/codec.h"
#include "protobuf/dispatcher.h"
#include "muduo/base/Logging.h"
using namespace std;
class Client
{
    typedef shared_ptr<mh::TranslateResponse> TranslateResponsePtr;
    typedef shared_ptr<mh::AddResponse> AddResponsePtr;

public:
    Client(const string &ip, const int port)
        : _latch(1), _client(_loopthread.startLoop(), muduo::net::InetAddress(ip, port), "Client"),
          _dispatcher(bind(&Client::onUnknownMessage, this, placeholders::_1, placeholders::_2, placeholders::_3)),
          _codec(bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, placeholders::_1, placeholders::_2, placeholders::_3))
    {
        _dispatcher.registerMessageCallback<mh::TranslateResponse>(bind(&Client::onTranslate, this, placeholders::_1, placeholders::_2, placeholders::_3));
        _dispatcher.registerMessageCallback<mh::AddResponse>(bind(&Client::onAdd, this, placeholders::_1, placeholders::_2, placeholders::_3));
        _client.setConnectionCallback(bind(&Client::onConnection, this, placeholders::_1));
        _client.setMessageCallback(bind(&ProtobufCodec::onMessage, &_codec, placeholders::_1, placeholders::_2, placeholders::_3));
    }
    void connect()
    {
        _client.connect();
        _latch.wait();
    }
    void translate(const string &msg)
    {
        mh::TranslateRequest resq;
        resq.set_msg(msg);
        _codec.send(_conn, resq);
    }
    void add(const int num1, const int num2)
    {
        mh::AddRequest resq;
        resq.set_num1(num1);
        resq.set_num2(num2);
        _codec.send(_conn, resq);
    }

private:
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected() == true)
        {
            _latch.countDown();
            _conn = conn;
            cout << "链接已建立: " << conn.get() << endl;
        }
        else
        {
            _conn.reset();
            cout << "链接已关闭" << endl;
        }
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        string msg = message->msg();
        cout << "翻译结果： " << msg << endl;
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        int result = message->result();
        cout << "加法结果： " << result << endl;
    }

private:
    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;
    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};
int main()
{
    Client client("127.0.0.1", 8088);
    client.connect();
    client.translate("世界");
    client.translate("Hello");
    client.add(10, 20);
    sleep(3);
    return 0;
}